
//引入自己封装后的request()
import { request } from '../../request/index.js'
//配置 支持async语法
import regeneratorRuntime from '../../lib/runtime/runtime'

// pages/goods_detail/index.js
Page({
  data: {
    goodsObj: {},  //商品详情数据
    isCollect: false,  //当前商品是否被收藏了
  },
  //商品详情数据对象
  GoodsInfo: {},

  // onLoad: function (options) {
  //   //1 取出页面参数 goods_id
  //   const { goods_id } = options
  //   //console.log(goods_id)

  //   //2 
  //   this.getGoodsDetail(goods_id)
  // },

  onShow () {
    //获取 页面栈数组
    let pages =  getCurrentPages();   
    //获取 当前页面对象
    let currentPage = pages[pages.length-1]
    //获取options
    let options = currentPage.options

    //1 取出页面参数 goods_id
    const { goods_id } = options
    //console.log(goods_id)

    //2 
    this.getGoodsDetail(goods_id)
  },

  //2 发起请求，获取商品详情数据
  async getGoodsDetail(goods_id){
    //发起请求，获取数据
    const res = await request({url:'/goods/detail', data: {goods_id}})
    //console.log(res)

    this.GoodsInfo = res.data.message

    //1 获取缓存中的商品收藏的数组
    let collect = wx.getStorageSync('collect') || []
    //2 判断当前商品是否被收藏
    let isCollect = collect.some(v => v.goods_id===this.GoodsInfo.goods_id)

    //保存数据
    this.setData({
      goodsObj: {
        pics: res.data.message.pics,
        goods_price: res.data.message.goods_price,
        goods_name: res.data.message.goods_name,
        goods_introduce: res.data.message.goods_introduce.replace(/\.webp/g, '.jpg'),
      },
      isCollect
    })
    //console.log(this.goodsObj)
  },

  //点击轮播图 预览大图
  handlePreviewImage(e){
    //console.log('~~~')

    //1 先构造要预览的图片数组
    const urls = this.GoodsInfo.pics.map(v=>v.pics_mid)

    //2 接收传递过来的图片url
    const current = e.currentTarget.dataset.url;

    wx.previewImage({
      current,
      urls
    });
  },

  //加入购物车
  handleCartAdd(){
    //console.log('加入购物车')

    //1获取缓存中的购物车数组
    let cart = wx.getStorageSync("cart") || []

    //2 判断 商品对象是否存在于购物车数组中
    let index = cart.findIndex(v=>v.goods_id === this.GoodsInfo.goods_id)

    if(index === -1){
      //3 不存在  第一次添加
      this.GoodsInfo.num = 1
      this.GoodsInfo.checked = true
      cart.push(this.GoodsInfo)
    }else{
      //4 已经存在 执行num++
      cart[index].num ++
    }

    //5 把购物车重新添加回缓存中
    wx.setStorageSync("cart", cart);

    //6 弹窗提示
    wx.showToast({
      title: '加入成功',
      icon: 'success',
      mask: true//true 防止用户手抖 疯狂点击按钮 1.5秒后才能再次点击
    });
      
  },

  //点击 收藏图标
  handleCollect(){
    let isCollect = false

    //1 获取缓存中的商品收藏数组
    let collect = wx.getStorageSync('collect') || [];

    //2 判断该商品是否已经被收藏了
    let index = collect.findIndex(v => v.goods_id===this.GoodsInfo.goods_id)

    //3 当 index != -1 表示己经收藏过
    if(index !== -1){
      //取消收藏
      collect.splice(index, 1)

      isCollect = false

      //提示 取消收藏
      wx.showToast({
        title: '取消收藏',
        icon: 'success',
        mask: true
      })

    }else{
      //收藏
      collect.push(this.GoodsInfo)
      isCollect = true

      //提示 收藏成功
      wx.showToast({
        title: '收藏成功',
        icon: 'success',
        mask: true
      })
        
    }
    //4 把收藏数组存入到缓存中
    wx.setStorageSync('collect', collect);

    //5 修改data中的属性 isCollect
    this.setData({ isCollect })

  }
})


